import { isString } from '../utils/index'

class Permission {
  constructor() {
    // 组
    this.state = [
      // {
      //   id: '', // 权限
      //   pid: '',
      //   name: '' // 权限名称
      // }
    ]
    this.instance = null
  }

  static getInstance() {
    if (!this.instance) {
      this.instance = new Permission()
    }
    return this.instance
  }

  /**
   * 设置
   * @param {*} item
   */
  setState(item) {
    if (!item) {
      return false
    }
    if (isString(item.pid)) {
      item['id'] = this.state.length
      let valid = this.state.some(v => {
        return v.pid === item.pid
      })
      if (!valid) {
        this.state.push(item)
        return true
      }
    }
    return false
  }
  /**
   * 获取
   * @param {string} pid
   */
  getState(pid) {
    return this.state.find(v => {
      return v.pid === pid
    })
  }
}

let _permission = Permission.getInstance()
export default _permission
